package com.zz.springboot.service.impl;

import com.zz.springboot.dto.MaxMoneyParamDto;
import com.zz.springboot.holder.ParamDataHolder;
import com.zz.springboot.manager.MaxMoneyParamManager;
import com.zz.springboot.service.SetParamService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SetParamServiceImpl implements SetParamService {

    @Autowired
    private MaxMoneyParamManager maxMoneyParamManager;

    @Override
    public MaxMoneyParamDto getMaxMoneyParam() {
        Session session = SecurityUtils.getSubject().getSession();
        String username = (String) session.getAttribute("username");
        return maxMoneyParamManager.getMaxMoneyParam(username);
    }

    @Override
    public void setMaxMoneyParam(MaxMoneyParamDto maxMoneyParam) {
        MaxMoneyParamDto dto = getMaxMoneyParam();
        if (dto == null){
            maxMoneyParamManager.insertMaxMoneyParam(maxMoneyParam);
        }else {
            maxMoneyParamManager.updateMaxMoneyParam(maxMoneyParam);
        }

        //更新缓存
        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
        ParamDataHolder.setMaxMoneyParamMap(username, maxMoneyParam);
    }
}
